﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using BLL;

namespace EDM
{
    public class RoleManage : IOperateDBRepository
    {

        TrainingWorkShopEntities dbContext = new TrainingWorkShopEntities();
        public List<BLL.Authority> GetAuthority()
        {

            //string sql = "select AuthorityId,AuthorityName from Authority";
            List<EDM.Authority> listDE = (from a in dbContext.Authorities select a).ToList<Authority>();
            List<BLL.Authority> authorityListBE = new List<BLL.Authority>();
            foreach (Authority authorityDE in listDE)
            {
                BLL.Authority authorityBE = Authoritytranslator.TranslatetoAuthorityBE(authorityDE);
                authorityListBE.Add(authorityBE);
            }
            return authorityListBE;
        }

        public BLL.Role GetRoleById(int roleId)
        {
            BLL.Role myRole = new BLL.Role();
            var role = (from m in dbContext.Roles where m.RoleId == roleId select m).FirstOrDefault();
            if (role == null)
            {
                myRole = null;
            }
            else
            {

                myRole = Roletranslator.TranslatetoRoleBE(role);
            }
            return myRole;

        }
 
        public object GetStatus(int role_temp)
        {
            //string select_statusid = "select StatusId from Role where RoleId='" + role_temp + "'";
            //return opDB.SelectS(select_statusid);

            string statusidEf = (from m in dbContext.Roles where m.RoleId == role_temp select m.StatusId).FirstOrDefault().ToString();
            int sta = Convert.ToInt32(statusidEf);
            return sta;
            
        }

        public object GetRoleName(int role_temp)
        {
            //string select_rolename = "select RoleName from Role where RoleId='" + role_temp + "'";
            //return opDB.SelectS(select_rolename);
            string selectRolename = (from m in dbContext.Roles where m.RoleId == role_temp select m.RoleName).FirstOrDefault().ToString();
            return selectRolename;

        }


        public List<BLL.RoleAuthority> GetAuthorityid(int role_temp)
        {
            var listDE = (from m in dbContext.RoleAuthorities where m.RoleId == role_temp select m).ToList();
            List<BLL.RoleAuthority> roleauthorityListBE = new List<BLL.RoleAuthority>();
            foreach (EDM.RoleAuthority roleDE in listDE)
            {
                BLL.RoleAuthority roleauthorityBE = RoleAuthoritytranslator.TranslatetoRoleAuthorityBE(roleDE);
                roleauthorityListBE.Add(roleauthorityBE);
            }
            return roleauthorityListBE;
        }
        

        public void GetUpdateRoleName(string TextBox, int role_temp)
        {
            //string update_rolename = "update Role set RoleName='" + TextBox + "' where RoleId=" + role_temp + "";
            //opDB.Execute(update_rolename);
            Role roleupdate = dbContext.Roles.First(u => u.RoleId == role_temp);
            roleupdate.RoleName = TextBox;
            dbContext.SaveChanges();
        }


        public int GetNewStatusid()
        {

            string statusname = "in use";
            string new_StatusId = (from m in dbContext.StatusTexts where m.StatusName == statusname select m.StatusId).FirstOrDefault().ToString();
            int newstatusid = Convert.ToInt32(new_StatusId);
            return newstatusid;
        }



        public void UpdateStatusId(int new_StatusidSelect, int role_temp)
        {
            //string update_StatusId = "update Role set StatusId=" + new_StatusidSelect + " where RoleId=" + role_temp + "";
            //opDB.Execute(update_StatusId);

            Role roleupdate = dbContext.Roles.First(u => u.RoleId == role_temp);
            roleupdate.StatusId = new_StatusidSelect;

            dbContext.SaveChanges();
        }

        public void DeleteAuthorityId(int role_temp)
        {
          
            var model = dbContext.RoleAuthorities.Where(d => d.RoleId == role_temp);
            if (model != null)
            {
                foreach (var item in model)
                {
                dbContext.DeleteObject(item);
                
                }
                dbContext.SaveChanges();
            }

        }

   
        public void InsertRoleAuthority(string str, int role_temp)
        {
            try
            {
                var newRoleAuthority = new RoleAuthority
                {
                    RoleId = role_temp,
                    AuthorityId = Convert.ToInt32(str)
                };
                dbContext.RoleAuthorities.AddObject(newRoleAuthority);
                dbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException.Message);
            }
        }

        
        public int GetNewStatusid1()
        {
            string statusname = "no use";
            string new_StatusId = (from m in dbContext.StatusTexts where m.StatusName == statusname select m.StatusId).FirstOrDefault().ToString();
            int newstatusid = Convert.ToInt32(new_StatusId);
            return newstatusid;

        }

        public void InsertRoleAuthority(int StatusId_Select, string TextBox)
        {
            //string insert_SelectStatusId = "insert into role(StatusId,RoleName) values(" + StatusId_Select + ",'" + TextBox + "')";//把查询出来的statusid插到role表格
            //opDB.Execute(insert_SelectStatusId);
            var newRole = new Role
            {
                StatusId = StatusId_Select,
                RoleName = TextBox
            };
            dbContext.Roles.AddObject(newRole);
            dbContext.SaveChanges();
        }

        public object GetRoleId(string TextBox1)
        {
            //string select_RoleId = "select RoleId from Role where RoleName='" + TextBox1 + "'";
            //return opDB.SelectS(select_RoleId);
            string select_RoleId = (from m in dbContext.Roles where m.RoleName == TextBox1 select m.RoleId).FirstOrDefault().ToString();
            return select_RoleId;

        }

        public string SelectRoleIdInput(string TextBox_input)
        {
            //string select_RoleIdInput = "select RoleId from Role where RoleId='" + TextBox_input + "'";
            //return opDB.SelectS(select_RoleIdInput);
            int textboxint = Convert.ToInt32(TextBox_input);
            // string select_RoleIdInput = (from m in dbContext.Roles where m.RoleId == textboxint select m).FirstOrDefault<Role>().RoleId.ToString();
          
            var roles = (from m in dbContext.Roles where m.RoleId == textboxint select m.RoleId);
            if (textboxint==0)
            {
                return "0";
            }
            if (roles.Count() == 0)
            {
                return null;
            }
            else
            {
                return roles.FirstOrDefault().ToString();
            }
        }



        public void UpdateRole(BLL.Role role)
        {
            var roleDE = dbContext.Roles.Where(r => r.RoleId == role.RoleId).FirstOrDefault();
            roleDE.RoleName = role.RoleName;
            roleDE.StatusId = (int)role.StatusId;
            roleDE.RoleAuthorities.Clear();
            foreach (BLL.Authority item in role.Authorities)
            {
                var newRoleAuthority = new RoleAuthority
                {
                    RoleId = role.RoleId,
                    AuthorityId = item.AuthorityId
                };
                dbContext.RoleAuthorities.AddObject(newRoleAuthority);
            }
            dbContext.SaveChanges();
        }


        public void InsertRole(BLL.Role role)
        {
            var newRole = new EDM.Role();
            newRole.RoleName = role.RoleName;
            newRole.StatusId = (int)role.StatusId;
            dbContext.Roles.AddObject(newRole);
            foreach (BLL.Authority item in role.Authorities)
            {
                var newRoleAuthority = new RoleAuthority
                {
                    RoleId = newRole.RoleId,
                    AuthorityId = item.AuthorityId
                };
                dbContext.RoleAuthorities.AddObject(newRoleAuthority);

            }
            dbContext.SaveChanges();
        }
    }
}